package com.yanqu.road.server.cmd.activity.washe;

import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.activity.washe.data.WasheUserData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.WasheProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.washe.WasheModule;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.pb.WashePb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_WASHE_QUIT_GUIDE, desc = "")
public class WasheQuitGuideCmd extends AbstractCommand {

    private int type = 1;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        WasheProto.WasheQuitGuideReqMsg reqMsg = WasheProto.WasheQuitGuideReqMsg.parseFrom(packet.getBytes());
        if (reqMsg.hasType()) {
            type = reqMsg.getType();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (WasheMgr.getWasheConfig() == null) {
            ret = GameErrorCode.E_WASHE_NO_SYSTEM;
            return;
        }
        WasheModule washeModule = player.getModule(WasheModule.class);
        if (washeModule.getWasheUserData() == null) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        ret = washeModule.quitGuideByCmd(type);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            WasheProto.WasheQuitGuideRespMsg.Builder builder = WasheProto.WasheQuitGuideRespMsg.newBuilder();
            builder.setRet(ret);
            WasheConfig config = WasheMgr.getWasheConfig();
            WasheUserData userData = player.getModule(WasheModule.class).getWasheUserData();
            if (userData != null && config != null) {
                builder.setUserData(WashePb.parseWasheUserDataMsg(config, userData, player.getModule(WasheModule.class).getWasheUserJinBiaoData(), null));
            }
            player.sendPacket(ClientProtocol.U_WASHE_QUIT_GUIDE, builder);
        }
    }
}
